package com.thenewboston.andy;

import java.util.List;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;

;

public class LocationActivity extends Activity implements OnClickListener {

	// Declaring variables
	Location location;
	LocationManager lm;
	LocationListener locationListener;
	TextView txtLat, txtLong, txtDistance, savedLat, savedLong;
	Button saveValues;
	private SharedPreferences pref1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);
		pref1=this.getSharedPreferences("LocDetails", Context.MODE_WORLD_READABLE);
		// use locationManager class to obtain locations data
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationListener();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				locationListener);
		lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		// linking to xml
		txtLat = (TextView) findViewById(R.id.tvLat);
		txtLong = (TextView) findViewById(R.id.tvLong);
		txtDistance = (TextView) findViewById(R.id.txtDistance);
		saveValues = (Button) findViewById(R.id.btnSave);
		saveValues.setOnClickListener(this);

		savedLat = (TextView) findViewById(R.id.savedLat);
		savedLat.setTextColor(Color.GRAY);
		savedLong = (TextView) findViewById(R.id.savedLong);
		savedLong.setTextColor(Color.GRAY);

		// print out all the location providers
		List<String> locationProviders = lm.getAllProviders();
		for (String provider : locationProviders) {
			Log.d("location Providers", provider);
		}

		// Specify a set of criteria to determine the best location provider
		Criteria c = new Criteria();
		c.setAccuracy(Criteria.ACCURACY_FINE);
		c.setAltitudeRequired(false);
		c.setBearingRequired(false);
		c.setCostAllowed(true);
		c.setPowerRequirement(Criteria.POWER_HIGH);

		// OS decides best provider based on criteria above
		String bestProvider = lm.getBestProvider(c, true);
		Log.d("location Providers", "Best provider is " + bestProvider);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		lm.removeUpdates(locationListener);

	}

	class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if (location != null) {
				txtLat.setText("Current Location Lat: "
						+ location.getLatitude());
				txtLong.setText("Current Location Long: "
						+ location.getLongitude());

				double currentLatitude = location.getLatitude();
				double currentLongitude = location.getLongitude();
				
				
				//declare shared preferences editor
				SharedPreferences.Editor edit = pref1.edit();
				edit.putString("Latitude", Double.valueOf(location.getLatitude()).toString());
				edit.putString("Longitude", Double.valueOf(location.getLongitude()).toString());;
				
				edit.commit();

				// Next thing to do is save current lat long values

				double endLatitude = 52.26938;
				double endLongitude = -9.70295;

				float[] resultFloat = new float[3];
				Location.distanceBetween(currentLatitude, currentLongitude,
						endLatitude, endLongitude, resultFloat);

				// Converting float result to readable string
				String[] resultString = new String[resultFloat.length];
				for (int i = 0; i < resultFloat.length; i++) {
					resultString[i] = "" + resultFloat[i];

					// converting meters to miles
					float mile = (float) 0.000621371192;
					float metersToMiles = (float) (resultFloat[0] * mile);
					String metresMiles = Float.toString(metersToMiles);

					// The computed distance is stored in results[0].
					txtDistance.setText("Hard coded distance result\n"
							+ resultString[0] + " Metres\n"
							+ (String) metresMiles + " Miles");
					
					

				}
			}

		}

		@Override
		public void onProviderDisabled(String provider) {
			Toast.makeText(getBaseContext(),
					"Provider: " + provider + "Disabled", Toast.LENGTH_SHORT)
					.show();

		}

		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(getBaseContext(),
					"Provider: " + provider + "Enabled", Toast.LENGTH_SHORT)
					.show();

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			String statusString = "";
			switch (status) {
			case android.location.LocationProvider.AVAILABLE:
				statusString = "Available";
			case android.location.LocationProvider.OUT_OF_SERVICE:
				statusString = "Out of service";
			case android.location.LocationProvider.TEMPORARILY_UNAVAILABLE:
				statusString = "Tempoarily unavailable";
			}
			Toast.makeText(getBaseContext(), provider + " " + statusString,
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		try {
			
			String latitudeString = pref1.getString("Latitude", "0");
			String longitudeString = pref1.getString("Longitude", "0");
			savedLat.setText(latitudeString + " Saved");
			savedLong.setText(longitudeString + " Saved");
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			

		
	}
	
}
